# Feel free to add "#! /bin/sh" to the top of the script. I prefer
# it without...
#
# Filename      : clonee.sh
# Author        : Christopher Steel
# Organization  : "Fair Access" not for profit in process of incorporation.
# Date          : 2010-02-26
# License       : MIT
#
# If you have feedback, suggestions, improvments or other scripts / information
# you feel would be helpful you can contact me at chris dot steel
# at gmail dot com for the time being.... Thanks!
#
# Metadata:
#
# Development
# Mercurial
# Web2py
# Universal Cake
#
# Tested on:
#
#		- Ubuntu
#			- 8.04 Hardy
#		- OSX
#			- 10.4.11
#
# Description:
#
#      - checks for any pre-existing local ephemeral clones and if found
#		   moves it to another directory and tags it with the current date and
#         time and the backup tag as set in the script.
# 		- makes a local "ephemeral" clone of another local (pristine) repository
#		- adds "_ephemeral to the end to the new clones repository name.
#
# Special notes:
#
# Currently customised for google code projects beginning with "uc_" but this
# can easily be by adjusting the following two variables:
#
#   SOURCE_APP_NAME=
#   TARGET_EPHEMERAL_APP_NAME=
#
# Notes:
#
# TODO: Add logging and/or logging script...
#
SCRIPT_NAME=`basename ${0} .sh`                             # script name with no .sh at the end

#######################
# Repository Variables
#######################
APP_NAME=${1}
#
# You probably want to adjust the following two variables
# to match your projects naming scheme.
#
SOURCE_APP_NAME=uc_${APP_NAME}                         ### Change to match your setup.
TARGET_EPHEMERAL_APP_NAME=${SOURCE_APP_NAME}_ephemeral ### Change for your setup

CWD=`pwd`
W2P_WORKING_DIR=${CWD}       # You might want to manually change this to reflect your setup
														# in some rare cases 
LOCAL_SOURCE_REPOSITORY_URL=${W2P_WORKING_DIR}/${SOURCE_APP_NAME}
LOCAL_TARGET_REPOSITORY_URL=${W2P_WORKING_DIR}/${TARGET_EPHEMERAL_APP_NAME}

# DATE_AND_TIME_STAMP=$(date +%Y-%m-%dT%T) #not valid as web2py app title
DATE_AND_TIME_STAMP=$(date +D_%Y_%m_%d_T_%H_%M_%S) # valid as web2py app title international,sorts nicely
BACKUP_TAG="${SCRIPT_NAME}_backup"
###################
# Variable Testing
###################
#
if [ "${1}" = "test" ]
    then
    echo "##############################################"
    echo "# Variable Testing"
    echo "##############################################"
    echo "#"
    echo "APP_NAME = ${APP_NAME}"
    echo ""
    echo "SOURCE_APP_NAME = ${SOURCE_APP_NAME}"
    echo "TARGET_EPHEMERAL_APP_NAME = ${TARGET_EPHEMERAL_APP_NAME}"
    echo ""
    echo "W2P_WORKING_DIR = ${W2P_WORKING_DIR}"
    echo "LOCAL_SOURCE_REPOSITORY_URL = ${LOCAL_SOURCE_REPOSITORY_URL}"
    echo "LOCAL_TARGET_REPOSITORY_URL = ${LOCAL_TARGET_REPOSITORY_URL}"
    echo ""
    echo "DATE_AND_TIME_STAMP = ${DATE_AND_TIME_STAMP}"
    echo "BACKUP_TAG = ${BACKUP_TAG}"
    echo ""
    echo "##############################################"
    exit 1
fi
###################
# Usage message
###################
if [ ! $1 ]
then
    echo "##############################################"
    echo "# Usage"
    echo "##############################################"
    echo "${SCRIPT_NAME} requires at least one command"
    echo "line option, 'test' or the name of a project"
    echo ""
    echo "Usage Example:"
    echo "      ${SCRIPT_NAME} blank"
    echo ""
    echo "Testing Example:"
    echo ""
    echo "      ${SCRIPT_NAME} test"
    echo ""
    echo "NOTES:"
    echo "This script adds the prefix 'uc_' to the"
    echo "project name. You may want to change the"
    echo "variables SOURCE_APP_NAME and "
    echo "TARGET_APP_NAME to match your projects "
    echo "naming scheme."
    echo "##############################################"
    exit 1
fi

###################
# Our Code actions
###################
cd ${W2P_WORKING_DIR}                       # change to our working directory
###############################
# backup any previous versions
###############################
if [ -d ${LOCAL_TARGET_REPOSITORY_URL} ]
then
    mv ${LOCAL_TARGET_REPOSITORY_URL} ${LOCAL_TARGET_REPOSITORY_URL}_${DATE_AND_TIME_STAMP}_${BACKUP_TAG}
    echo "moved ${LOCAL_TARGET_REPOSITORY_URL} to "
    echo "${LOCAL_TARGET_REPOSITORY_URL}_${DATE_AND_TIME_STAMP}_${BACKUP_TAG}"
    # need log command here
fi
########
# Clone 
########
hg clone ${SOURCE_APP_NAME} ${TARGET_EPHEMERAL_APP_NAME}
 
############
# That's it
############
echo "completed"

